/**
 * 
 */
package Generators;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

import javax.xml.bind.DatatypeConverter;

/**
 * @author ASUS
 *
 */
public class DSAPublicKey extends DSAKey{
	private BigInteger y;
	

	public DSAPublicKey(DSAParameters dsaParameters, BigInteger pubKey) {
		super(dsaParameters);
		y = pubKey;
	}
	
	public String toString(){
		//Get DSA Parameters's informations
		StringBuffer sf = new StringBuffer(getParameters().toString());
		//Append private key's informations
		sf.append("Private Key\n");
		sf.append("\tSize : " + y.bitLength() + "\n");
		sf.append("\tValue: " + y.toString(16) + "\n");
		
		return sf.toString();
	}
	
	/* (non-Javadoc)
	 * @see Generators.DSAKey#exportToFile(java.lang.String)
	 */
	@Override
	public int exportToFile(String fileName) {
		Charset charset = Charset.forName("US-ASCII");
		try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName), charset, StandardOpenOption.CREATE)) {
			String s = toString();
			writer.write("-----BEGIN PUBLIC KEY-----\n");
			writer.write(DatatypeConverter.printBase64Binary(s.getBytes()));
			//writer.write(s);
			writer.write("\n-----END PUBLIC KEY-----\n");
			writer.close();
			return 1;		
		} catch (IOException x) {
		    System.err.format("IOException: %s%n", x);
			return -1;
		}
	}

	/* (non-Javadoc)
	 * @see Generators.DSAKey#getValue()
	 */
	@Override
	public BigInteger getValue() {
		// TODO Auto-generated method stub
		return y;
	}

}
